package com.medical.project.system.controller;

import java.util.List;

import com.medical.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medical.framework.aspectj.lang.annotation.Log;
import com.medical.framework.aspectj.lang.enums.BusinessType;
import com.medical.project.system.domain.FamilyMember;
import com.medical.project.system.service.IFamilyMemberService;
import com.medical.framework.web.controller.BaseController;
import com.medical.framework.web.domain.AjaxResult;
import com.medical.common.utils.poi.ExcelUtil;
import com.medical.framework.web.page.TableDataInfo;

/**
 * 家庭成员Controller
 *
 * @author tale
 * @date 2020-06-20
 */
@RestController
@RequestMapping("/system/member")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;

    /**
     * 查询家庭成员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(FamilyMember familyMember) {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        return getDataTable(list);
    }

    /**
     * 查询家庭未参合成员
     *
     * @param familyCode
     * @return
     */
    @GetMapping("/noJoin")
    public FamilyMember selectNoJoin(String familyCode) {


        return familyMemberService.selectNoJoin(familyCode);
    }

    /**
     * 导出家庭成员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:export')")
    @Log(title = "家庭成员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FamilyMember familyMember) {
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
        return util.exportExcel(list, "member");
    }

    /**
     * 获取家庭成员详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(familyMemberService.selectFamilyMemberById(id));
    }

    /**
     * 新增家庭成员
     */
    @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "家庭成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.insertFamilyMember(familyMember));
    }

    /**
     * 更新参合状态
     *
     * @param idCard
     * @return
     */
    @GetMapping("/updateState")
    public AjaxResult updateState(String idCard) {
        if (StringUtils.isEmpty(idCard)){
            return AjaxResult.error();
        }

        int flag = familyMemberService.updateState(idCard);
        if (flag > 0) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改家庭成员
     */
    @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @Log(title = "家庭成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.updateFamilyMember(familyMember));
    }

    /**
     * 删除家庭成员
     */
    @PreAuthorize("@ss.hasPermi('system:member:remove')")
    @Log(title = "家庭成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(familyMemberService.deleteFamilyMemberByIds(ids));
    }
}
